#include"Delay.h"

// 微秒定时器
void Delay_us(uint16_t us)
{
    // 给定时器赋初值
    SysTick->LOAD |= 71 * us;
    // 每次结束计时给value清零
    SysTick->VAL = 0;
    // 选择时钟源，关闭中断，开启定时器

    SysTick->CTRL |= SysTick_CTRL_CLKSOURCE;
    SysTick->CTRL &= ~(SysTick_CTRL_TICKINT);
    SysTick->CTRL |= SysTick_CTRL_ENABLE;

    while ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG) == 0)
    {
    }
    SysTick->CTRL &= ~SysTick_CTRL_COUNTFLAG;
    SysTick->CTRL &= ~SysTick_CTRL_ENABLE;
}

// 毫秒定时器
void Delay_ms(uint16_t ms)
{
    while (ms--)
    {
        Delay_us(1000);
    }
}

// 秒定时
void Delay_s(uint16_t s)
{
    while (s--)
    {
        Delay_ms(1000);
    }
}
